package com.xz.db;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.xz.db.core.BaseEntity;
import com.xz.db.core.IBaseMapper;
import com.xz.db.core.IServicePlus;
import com.xz.db.core.ServicePlusImpl;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * @author cuizh
 * @version 1.0
 * @date 2020-11-09
 */
public class CodeGenerator {

    /**
     * <p>
     * 读取控制台内容,啊
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    //https://github.com/baomidou/generator/blob/develop/mybatis-plus-generator/src/test/java/com/baomidou/mybatisplus/generator/samples/H2CodeGeneratorTest.java
    public static void main(String[] args) {
        gen1();
    }




    public static void gen1(){
        String projectPath = System.getProperty("user.dir")+"/moonce-common/moonce-mybatis";
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/cloudalibaba?autoReconnect=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true",
                        "root", "0000")
                .globalConfig(builder -> {
                    builder.author("pzy") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(projectPath + "/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.boot") // 设置父包名
                            .moduleName("server") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath+"/src/main/resources/mapper/")); // 设置mapperXml生成路径
                })
                .strategyConfig((scanner, builder) -> builder
//                        .addInclude(getTables("all"))  //请输入表名，多个英文逗号分隔？所有输入 all
                        .addInclude(getTables("xz_book"))  //请输入表名，多个英文逗号分隔？所有输入 all
                        .enableSkipView()
                        .controllerBuilder().formatFileName("")//为空表示不生产
                        .serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl").superServiceClass(IServicePlus.class)
                        .superServiceImplClass(ServicePlusImpl.class)//为空表示不生产
                        .mapperBuilder().fileOverride().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper").superClass(IBaseMapper.class)
                        .entityBuilder().enableLombok()
//                        .enableTableFieldAnnotation()
                        .addTableFills(
                                new Column("created_time", FieldFill.INSERT),
                                new Column("updated_time", FieldFill.INSERT_UPDATE)
                        ).fileOverride().superClass(BaseEntity.class)
                        .build())
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }



}
